package com.jye.cloud.user.service.impl;


import com.jye.cloud.Constants.MyConstants;
import com.jye.cloud.domain.Like;
import com.jye.cloud.user.service.LikeService;
import com.jye.cloud.user.mapper.LikeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LikeServiceImpl implements LikeService {
    @Autowired
    LikeMapper likeMapper;

    @Override
    public int findLikeCountByTypeAndPId(int type, int pId) {
        Like like = new Like();
        like.setStatus(MyConstants.isLikeOrFollow.yes);
        like.setType(type);
        like.setPId(pId);
        int count = likeMapper.findLikeCountByLike(like);
        return count;
    }

    @Override
    public int doLike(Like like) {
        try {
            //查询记录
            int i = likeMapper.isExist(like);
            if (i == 0) {
                //无记录,插入Like
                likeMapper.addLike(like);
            } else {
                //已有记录,更新status
                likeMapper.updateStatus(like);
            }
            //无异常
            return 1;
        } catch (Exception e) {
            return 0;
        }

    }
}
